package com.hyt.it.ogt.kq.main.controller.bm;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.model.vo.StudentListVo;
import com.hyt.it.ogt.kq.service.bm.service.ISendMessageService;
import com.hyt.it.ogt.kq.service.gov.model.param.MessageParam;
import com.hyt.it.ogt.kq.service.gov.model.param.MessageQueryParam;
import com.hyt.it.ogt.kq.service.gov.model.param.fjCityBm.SendMessageParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.SendMessageVo;
import com.hyt.loginfo.service.IBaseLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Set;

/**
 * @author liteng
 * @since 2024-01-24
 */
@Api(tags = "46.发送短信")
@RestController
@ApiSort(value = 46)
@RequestMapping("/sendMessage")
@Slf4j
public class SendMessageController extends BaseController {

    @Autowired
    ISendMessageService iSendMessageService;

    @Resource
    IBaseLoginService iBaseLoginService;

    @ApiOperation(value = "46.1.1 发送短信", notes = "20000:成功")
    @ApiOperationSort(value = 1)
    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
    public ApiResponse<Object> sendMessage(@RequestBody MessageParam messageParam) {
        Set<String> deptIds = iBaseLoginService.getDeptIds();
        messageParam.setDeptIds(deptIds);
        iSendMessageService.sendMsg(messageParam,getUserId(),getOfficeId());
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
    }

    @ApiOperation(value = "46.1.2 获取短信模板", notes = "20000:成功")
    @ApiOperationSort(value = 2)
    @RequestMapping(value = "/queryMessageTemplateList", method = RequestMethod.GET)
    public ApiResponse<Object> queryMessageTemplateList() {
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(iSendMessageService.findSendMessageTemplateList(getOfficeId())).build();
    }

    @ApiOperation(value = "46.1.3 获取需要发送的总短信数", notes = "20000:成功")
    @ApiOperationSort(value = 3)
    @RequestMapping(value = "/getTotalMessageNum", method = RequestMethod.GET)
    public ApiResponse<Object> getTotalMessageNum(MessageParam messageParam) {
        if (StrUtil.isBlank(messageParam.getMessageContent())) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(0).build();
        }
        Set<String> deptIds = iBaseLoginService.getDeptIds();
        messageParam.setDeptIds(deptIds);
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(iSendMessageService.querySendMessageNum(messageParam)).build();
    }

    @ApiOperation(value = "46.1.4 获取短信分页", notes = "20000:成功")
    @ApiOperationSort(value = 4)
    @RequestMapping(value = "/queryMessage", method = RequestMethod.GET)
    public ApiResponse<IPage<SendMessageVo>> queryMessage(MessageQueryParam param){
        IPage<SendMessageVo> page = iSendMessageService.queryMessage(param);
        return ApiResponse.<IPage<SendMessageVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(page)
                .build();
    }

}
